//
//  LFLocalUserManager.m
//  shareProDemo
//
//  Created by Xiao Xiao on 2018/9/4.
//  Copyright © 2018年 xiaoxiao. All rights reserved.
//

#import "CBTLocalUserManager.h"
#import "NSString+Category.h"
NSString * const kLFLogOutSuccessNotificationName = @"kLFLogOutSuccessNotificationName";
NSString * const kLFLogInSuccessNotificationName = @"kLFLogInSuccessNotificationName";

NSString * const kUserIDKey = @"userId";
NSString * const kUserInfoKey = @"data";
NSString * const kStarCoinKey = @"userExtend";

@implementation CBTLocalUserManager
+ (CBTLocalUserManager *)sharedInstance
{
    static CBTLocalUserManager * sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[CBTLocalUserManager alloc] init];
    });
    return sharedInstance;

}

- (void) saveALLUserInfoDic:(NSDictionary *)dic{
    if ([dic isKindOfClass:[NSDictionary class]] == NO) {
        return;
    }
    if ([dic isKindOfClass:[NSDictionary class]] == NO) return;
    if ([dic.allKeys containsObject:kUserInfoKey]) {
        if ([[dic[@"data"]objectForKey:@"asset"]floatValue] <= 0) {
            [dic[@"data"] setObject:@"0" forKey:@"asset"];
        }
        [[NSUserDefaults standardUserDefaults] setObject:dic[@"data"] forKey:kUserInfoKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)refreshUserInfoDic:(NSDictionary *)dic{
    NSMutableDictionary *userDic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey]];
    if (userDic.allKeys >0 && dic.allKeys>0) {
        for (NSString *keyStr in dic.allKeys) {
            [userDic setObject:dic[keyStr] forKey:keyStr];
        }
    }
    [self saveALLUserInfoDic:@{kUserInfoKey:userDic}];
}

- (void)removeUserInfo{
    
    [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:HEFANTV_TOKEN];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:HEFANTV_PassWord];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:HEFANTV_PayPassWord];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey]) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserInfoKey];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
}


- (NSDictionary *)userDIc{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey] ;
}

- (BOOL)isLogIn{
    return ![NSString isBlankString:HEFANTV_DEVICETOKEN];
}

- (NSString *)userId{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"userId"]];
}

- (NSString *)nickName{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"nickName"]];
}

- (NSString *)headImage{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"picUrl"]];
}

- (NSString *)userAsset{
    return [NSString numberExcludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"asset"]];
}

- (NSString *)avatar{
    return [NSString numberExcludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"avatar"]];
}

- (NSString *)payPassword{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"payPassword"]];
}

- (NSString *)walletAddress{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"walletAddress"]];
}

- (NSString *)passWord{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"passWord"]];
}

- (NSString *)userEmail{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"mailNum"]];
}

- (NSString *)userPhone{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"phone"]];
}

- (NSString *)flag{
     return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"flag"]];
}

- (NSString *)status{
     return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"status"]];
}

- (NSString *)signInFlag{
    return [NSString excludeNullString:[[NSUserDefaults standardUserDefaults] objectForKey:kUserInfoKey][@"signInFlag"]];
}

- (BOOL)address {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"address"];
}

@end
